package com.jule.goldfast.base.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Joiner;
import com.jule.core.utils.Base64;
import com.jule.core.utils.HttpsUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

public class PlatformApi {
    /** logger */
    private final static Logger log = LoggerFactory.getLogger(PlatformApi.class);

    /**
     * 查询用户是否在游戏中
     * @param identity
     * @return
     */
    public static JSONObject queryUserInGame(String identity) {
        String url = "http://hall.api.com:9104/HallService/queryUserInGame";
        Map<String, Object> param = new HashMap<>();
        param.put("identity",identity);
        return doGet(url,param);
    }

    /**
     * 登录游戏
     * @param identity
     * @param gameId
     * @return
     */
    public static JSONObject loginGame(String identity,String gameId) {
        String url = "http://hall.api.com:9104/HallService/loginGame";
        Map<String, Object> param = new HashMap<>();
        param.put("identity",identity);
        param.put("game_id",gameId);
        return doGet(url,param);
    }

    /**
     * 登出游戏
     * @param identity
     * @param gameId
     * @return
     */
    public static JSONObject logoutGame(String identity,String gameId) {
        String url = "http://hall.api.com:9104/HallService/logoutGame";
        Map<String, Object> param = new HashMap<>();
        param.put("identity",identity);
        param.put("game_id",gameId);
        return doGet(url,param);
    }

    /**
     * 通过身份串获取用户信息
     * @param identity
     * @return
     */
    public static JSONObject getByIdentity(String identity) {
        String url = "http://account.api.com:9001/UserService/getByIdentity";
        Map<String, Object> param = new HashMap<>();
        param.put("identity",identity);
        return doGet(url,param);
    }

    /**
     * 更新游戏在线用户数(定时更新)
     * @param gameId
     * @param onlineNumber
     * @return
     */
    public static JSONObject updateOnlineNum(String gameId,Integer onlineNumber) {
        String url = "http://account.api.com:9001/UserService/getByIdentity";
        Map<String, Object> param = new HashMap<>();
        param.put("game_id",gameId);
        param.put("onlinenumber",onlineNumber);
        return doGet(url,param);
    }

    private static JSONObject doGet(String url,Map<String,Object> param){
        try {
            String str = Joiner.on("&").skipNulls().join(param.entrySet());
            url = Joiner.on("?").skipNulls().join(url, str);
            String result = HttpsUtil.doGet(url,true);
            log.debug("current url："+url);
            log.info("get result = "+result);
            if (result != null) {
                return JSONObject.parseObject(result);
            }
        }catch (Exception e){
            log.error(e.getMessage());
        }
        return null;
    }

    public static void main(String[] args) {
//        JSONObject jsonObject = getIdentity("1026659612296","8888");
//        JSONObject json = (JSONObject) jsonObject.get("result");
//        System.out.println(String.valueOf(json.get("identity")));
//        queryUserInGame("aksjhdwheifuhwiefnjksdfnskdf");
//        loginGame("aksjhdwheifuhwiefnjksdfnskdf","1001");
//        logoutGame("aksjhdwheifuhwiefnjksdfnskdf","1001");

//        JSONObject jsonObject = (JSONObject) JSON.parseObject("{\"code\":0,\"msg\":\"\",\"result\":{\"user_obj\":{\"id\":\"1_test002\",\"username\":\"1_test002\",\"nickname\":\"\\u7528\\u6237_test002\",\"channel_id\":\"1\",\"platform\":\"ios\",\"icon\":\"\",\"gold\":1000000,\"valid_gold\":0,\"token\":\"THCG\",\"create_time\":\"2018-11-08 10:07:23\",\"invite_code\":\"CPLZ\"}},\"time\":\"2018-11-08 10:20:17\"}").get("result");
//        JSONObject playerJson = (JSONObject) jsonObject.get("user_obj");
//        String userId = playerJson.getString("id");
//        log.debug("player id: {}",userId);
//        System.out.println(playerJson.getFloatValue("gold"));
//        System.out.println(playerJson.getString("nickname"));

        String url = "https://api.wl860.com/Api/test";
        Map<String,Object> map = new HashMap<>();
        map.put("username","panghu");
        map.put("money",10000);

        String strs = Joiner.on("&").skipNulls().join(map.entrySet());
        url = Joiner.on("?").skipNulls().join(url, strs);
        String result = HttpsUtil.doGet(url,true);
        String identity = JSONObject.parseObject(result).getJSONObject("result").getString("identity");

        System.out.println(identity);

        Object obj = "{\"entrance\":[\"3dfishhall.wl860.com\",\"3dfishhall.wl960.com\"],\"chat\":\n" +
                " \n" +
                "[\"chat.wl860.com\",\"chat.wl960.com\"],\"identity\":"+identity+",\"channel_id\":\"1\",\"return_url\":\"\"}";

        String str = obj.toString();
        System.out.println("----------"+str);
        try {
            String base64Sign = Base64.encodeToString(str.getBytes("UTF-8"),false);
            String strss = new String(Base64.decode(base64Sign));
            System.out.println();
            System.out.println("========"+strss);

            System.out.println(base64Sign);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}
